﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Giaiphongmatbang.BLL;
using Giaiphongmatbang.DataModel;
namespace Giaiphongmatbang
{
    public partial class editDuan : System.Web.UI.Page
    {
        int ProjectID
        {
            set { ViewState["ProjectID"] = value; }
            get { return (int)ViewState["ProjectID"]; }
        }
        QLGPMBEntities db = new QLGPMBEntities();
        protected void Page_Load(object sender, EventArgs e)
        {            
            if (!IsPostBack)
            {
                ProjectID = int.Parse(Request.QueryString["ID"]);
                if (Session["User"] != null)
                {
                    Project project = new ProjectsRepository().GetByID(ProjectID);
                    txtProjectName.Text = project.ProjectName;
                    txtInvestor.Text = project.Investor;
                    txtCosts.Text = project.Costs.ToString();
                    chkCompleted.Checked = project.isCompleted;
                    chkPublic.Checked = project.isPublic;
                    BindDataRepeater();
                }
            }           
        }

        protected void BindDataRepeater()
        {
            rptInvestor.DataSource = new ManagmentsRepository().GetByProjectID(ProjectID);
            rptInvestor.DataBind();
        }

        int ManagementID
        {
            get { return (int)ViewState["ManagementID"]; }
            set { ViewState["ManagementID"] = value; }
        }
        protected void rptInvestor_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            ManagementID = int.Parse(e.CommandArgument.ToString());
            ManagmentsRepository repository = new ManagmentsRepository();
            Managment manage = repository.GetByID(ManagementID);
            if (e.CommandName.Equals("btnDelete"))
            {
                repository.Delete(manage);
                BindDataRepeater();
            }
            else if (e.CommandName.Equals("btnEdit"))
            {
                hdfModeNew.Value = "false";
                txtFullName.Text = manage.FullName;
                txtOrder.Text = manage.Order;
                txtRepresentative.Text = manage.Representative;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "showdialog();", true);
            }
        }

        protected void btnInsert_Click(object sender, ImageClickEventArgs e)
        {
            hdfModeNew.Value = "true";
            txtFullName.Text = string.Empty;
            txtOrder.Text = string.Empty;
            txtRepresentative.Text = string.Empty;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "showdialog();", true);
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            bool isModenew = Convert.ToBoolean(hdfModeNew.Value);
            ManagmentsRepository repository = new ManagmentsRepository();
            if (isModenew)
            {
                repository.Insert(ProjectID, txtFullName.Text, txtOrder.Text, txtRepresentative.Text);
            }
            else
            {                
                Managment manage = repository.GetByID(ManagementID);
                manage.FullName = txtFullName.Text;
                manage.Order = txtOrder.Text;
                manage.Representative = txtRepresentative.Text;                
            }
            BindDataRepeater();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            int result = new DataAccess.GPMBDataContext().uspProjectUpdate(ProjectID, txtInvestor.Text, txtProjectName.Text, double.Parse(txtCosts.Text.Trim()), chkCompleted.Checked, chkPublic.Checked);
            if (result != 0)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('Có lỗi xảy ra khi cập nhật!');", true);
                return;
            }
            Response.Redirect("/duan.aspx");
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("/duan.aspx");
        }
    }
}